'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	let res = {
		success: false,
		message: '保存失败'
	}
	//event为客户端上传的参数
	console.log(event)
	// 选择表
	const yeardb = db.collection('year')
	const monthdb = db.collection('month')
	const daydb = db.collection('day')
	const converdb = db.collection('conver')
	//查询是否有年的纪录
	let data_year = await yeardb.where({
		object_id: event.obj_id,
		year: event.data.year
	}).get();
	// 如果没有进行添加操作
	if (!data_year.data[0]) {
		data_year = await yeardb.add({
			object_id: event.obj_id,
			year: event.data.year,
			y_name: '年' //暂时为年，后续自定义
		})
		data_year = await yeardb.where({
			object_id: event.obj_id,
			year: event.data.year
		}).get();
	}
	console.log(data_year)
	//查询是否有月的纪录
	let data_month = await monthdb.where({
		object_id: event.obj_id,
		year_id: data_year.data[0]._id,
		month: event.data.month
	}).get()
	// 如果没有进行添加操作
	if (!data_month.data[0]) {
		data_month = await monthdb.add({
			object_id: event.obj_id,
			year_id: data_year.data[0]._id,
			month: event.data.month,
			m_name: '月' //暂时，后续自定义
		})
		data_month = await monthdb.where({
			object_id: event.obj_id,
			year_id: data_year.data[0]._id,
			month: event.data.month
		}).get()
	}
	//查询是否有当天的纪录
	let sever_day = await daydb.where({
		object_id: event.obj_id,
		month_id: data_month.data[0]._id,
		day: event.data.day
	}).get()
	// 如果有进行更新操作
	if (sever_day.data[0]) {
		// 查询日记内容
		let data_con = await converdb.where({
			day_id: sever_day.data[0]._id
		}).get();
		// 获取更新时间
		let times = new Date();
		let hour = times.getHours().length + 8 === 2 ? times.getHours() + 8 : ('0' + times.getHours());
		let minutes = times.getMinutes().length === 2 ? times.getMinutes() : ('0' + times.getMinutes());
		let seconds = times.getSeconds().length === 2 ? times.getSeconds() : ('0' + times.getSeconds());
		// 简单格式化
		let mytimes = hour + ":" + minutes + ":" + seconds;
		// 设置更新标题
		let ok_conver = data_con.data[0].conver + "\n今天 " +
			mytimes + " 更新:" +
			event.data.titile + " \n" +
			event.data.conver;
			// 更新文件链接
			//图片和视频都有上传和只上传一类分开处理
		if (data_con.data[0].imgArryId.length > 0 && data_con.data[0].vidArryId.length > 0) {
			for (let i in event.data.img_id) {

				data_con.data[0].imgArryId.push(event.data.img_id[i])

			}
			for (let i in event.data.vid_id) {

				data_con.data[0].vidArryId.push(event.data.vid_id[i])

			}
		} else if (data_con.data[0].vidArryId.length > 0) {
			for (let i in event.data.vid_id) {

				data_con.data[0].vidArryId.push(event.data.vid_id[i])

			}
		} else if (data_con.data[0].imgArryId.length > 0) {
			for (let i in event.data.img_id) {

				data_con.data[0].imgArryId.push(event.data.img_id[i])

			}
		} else {
			//没有上传
			data_con.data[0].imgArryId = event.data.img_id
			data_con.data[0].vidArryId = event.data.vid_id
		}


		let ok_imgArryId = data_con.data[0].imgArryId
		let ok_vidArryId = data_con.data[0].vidArryId
		//更新内容
		data_con = await converdb.where({
			day_id: sever_day.data[0]._id
		}).update({
			conver: ok_conver,
			imgArryId: ok_imgArryId,
			vidArryId: ok_imgArryId
		});
		if (data_con) {
			res = {
				success: true,
				message: '保存成功',
				code: '0000',
				data: data_con
			}
		} else {
			res = {
				success: false,
				message: '保存失败！',
				code: '0000',
			}
		}
		return res
	}
	// 如果没有进行添加操作
	let data_day = await daydb.add({
		object_id: event.obj_id,
		month_id: data_month.data[0]._id,
		day: event.data.day,
		d_name: '日', //暂时，后续自定义
		title: event.data.titile
	})
	data_day = await daydb.where({
		object_id: event.obj_id,
		month_id: data_month.data[0]._id,
		day: event.data.day
	}).get()
	// 添加日记内容
	let data_con = await converdb.add({
		object_id: event.obj_id,
		day_id: data_day.data[0]._id,
		conver: event.data.conver,
		imgArryId: event.data.img_id,
		vidArryId: event.data.vid_id
	})
	data_con = await converdb.where({
		day_id: data_day.data[0]._id
	}).get()
	if (data_con) {
		res = {
			success: true,
			message: '保存成功',
			code: '0000',
			data: data_con
		}
	}
	//返回数据给客户端
	return res
};
